/**
 * ...
 * @author Krzysztof Rozalski
 */

package hapi.sprite.blit;

import flash.display.Graphics;
import flash.geom.Point;
import hapi.sprite.display.SpriteSheetDisplay;
import hapi.sprite.sheet.NMETileSpriteSheet;

class NMEBlitSpriteSheetDisplay implements SpriteSheetDisplay {

	public var visible(default, null):Bool;
	public var currentFrame(default, null):Int;
	public var totalFrames(default, null):Int;
	public var spriteSheet(default, null):NMETileSpriteSheet;
	public var x:Float;
	public var y:Float;
	var xyids:Array<Float>;
	var offset:Int;
	
	public function new(spriteSheet:NMETileSpriteSheet, xyids:Array<Float>, ?x:Float=0.0, ?y:Float=0.0) {
		this.spriteSheet = spriteSheet;
		this.totalFrames = spriteSheet.totalFrames;
		this.currentFrame = 1;
		this.visible = true;
		this.x = x;
		this.y = y;
		this.xyids = xyids;
		reallocate(xyids.length);
	}
	
	public inline function reallocate(offset:Int) {
		this.offset = offset;
		xyids.push(x);
		xyids.push(y);
		xyids.push(currentFrame-1);
	}
	
	public inline function isVisible():Bool {
		return visible;
	}
	
	public function show():Void {
		if(!isVisible()) {
			this.visible = true;
			xyids[offset + 2] = currentFrame-1;
		}
	}
	
	public function hide():Void {
		if(isVisible()) {
			this.visible = false;
			xyids[offset + 2] = -1;
		}
	}
	
	public function goto(frame:Int) {
		if(frame <= totalFrames && frame >= 1) {
			this.currentFrame = frame;
			if(isVisible()) xyids[offset + 2] = frame-1;
		}
	}
	
	public inline function moveTo(x:Float, y:Float):Void {
		this.x = xyids[offset] = x;
		this.y = xyids[offset+1] = y;
	}
}